package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaBuildingModel;
import com.wzt.lianfirecontrol.utils.StringUtils;

public class XunChaBuildAdapter extends BaseRecyclerAdapter<XunChaBuildingModel> {

    private Context context;

    private View.OnTouchListener onTouchListener;

    private boolean isHistory = false;

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public XunChaBuildAdapter(Context context) {
        this.context = context;
    }

    public View.OnTouchListener getOnTouchListener() {
        return onTouchListener;
    }

    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_build_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaBuildingModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_location;
        private RecyclerView rlv_floor_list;
        private XunChaFloorAdapter xunChaFloorAdapter;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.rlv_floor_list = itemView.findViewById(R.id.rlv_floor_list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        myViewHolder.rlv_floor_list.setLayoutManager(gridLayoutManager);
        myViewHolder.xunChaFloorAdapter = new XunChaFloorAdapter(context);
        myViewHolder.xunChaFloorAdapter.setHistory(isHistory);
        myViewHolder.rlv_floor_list.setAdapter(myViewHolder.xunChaFloorAdapter);
        if (null != onTouchListener) {
            myViewHolder.rlv_floor_list.setOnTouchListener(onTouchListener);
        }
    }

    private void setItemData(MyViewHolder viewHolder, XunChaBuildingModel data) {
        if (!StringUtils.isEmpty(data.getName())){
            viewHolder.tv_location.setVisibility(View.VISIBLE);
            viewHolder.tv_location.setText(data.getName());
        }else {
            viewHolder.tv_location.setVisibility(View.GONE);
        }

        if (null != data.getChildren() && data.getChildren().size() > 0){
            viewHolder.rlv_floor_list.setVisibility(View.VISIBLE);
            viewHolder.xunChaFloorAdapter.clear();
            viewHolder.xunChaFloorAdapter.addDatas(data.getChildren());
        }else {
            viewHolder.rlv_floor_list.setVisibility(View.GONE);
        }
    }
}
